package com.xinghou.platform.service.main.impl;

import com.xinghou.platform.dao.bases.redis.RedisOperator;
import com.xinghou.platform.dao.main.VersionConfigDAO;
import com.xinghou.platform.dao.main.common.DBUserLevelCached;
import com.xinghou.platform.model.main.VersionConfig;
import com.xinghou.platform.server.api.service.VersionConfigService;
import com.xinghou.platform.util.AppContextUtil;
import com.xinghou.platform.util.DateUtil;
import com.xinghou.platform.util.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by qingshengzheng on 16/5/20.
 */
@Service("versionConfigService")
public class VersionConfigServiceImpl implements VersionConfigService {
    private static String MAJIA_ACCOUNT_KEY = "majia_account_key";
    private static String MESSAGE_NOTIFY_KEY = "message_notify_key:%s";
    private static String YY_ACCOUNT_LOGINTIME = "yy_account_logintime:%s";
    private static RedisOperator redisOperator=null;

    @Resource
    private VersionConfigDAO versionConfigDAO;




     public static void  initRedis()
     {
         if(redisOperator==null)
             redisOperator=(RedisOperator) AppContextUtil.getBean("cachedMainRedisOperator");

     }

    @Override
    public List<String> getRangeMessNotify(String yyid, Long count) {
        initRedis();
        String key = String.format(MESSAGE_NOTIFY_KEY, yyid);
        List<String> values = redisOperator.rrange(key, 1, count, DBUserLevelCached.CACHED_DB_INDEX);
        redisOperator.ltrim(key, 0, -(count + 1), DBUserLevelCached.CACHED_DB_INDEX);
        recordYyLogin(yyid);
        return values;
    }

    @Override
    public void addMessageNotifyRel(String uid,String mjuid,String nickname,String mjnickname, String yyid) {
        initRedis();
        String key = String.format(MESSAGE_NOTIFY_KEY, yyid);
        String value=uid+"::"+mjuid+"::"+nickname+"::"+mjnickname;
        redisOperator.lrem(key, 0l, DBUserLevelCached.CACHED_DB_INDEX, value);
        redisOperator.lpush(key, value, DBUserLevelCached.CACHED_DB_INDEX);
    }

    @Override
    public void addMessageNotifyRel(String uid, String yyid) {
        initRedis();
        String key = String.format(MESSAGE_NOTIFY_KEY, yyid);
        redisOperator.lrem(key, 0l, DBUserLevelCached.CACHED_DB_INDEX, uid);
        redisOperator.lpush(key, uid, DBUserLevelCached.CACHED_DB_INDEX);

    }
    public void recordYyLogin(String yyid) {
        initRedis();
        String key = String.format(YY_ACCOUNT_LOGINTIME, yyid);
        Long time = DateUtil.getTimeStampByDate(new Date());
        redisOperator.set(key, time.toString(), DBUserLevelCached.CACHED_DB_INDEX);
    }

    @Override
    public VersionConfig getVersionConfigByOsType(byte osType) throws DataAccessException {
       return versionConfigDAO.getVersionConfigByOsType(osType);
    }

    @Override
    public int updateVersionConfigByOsType(VersionConfig versionConfig) {
        return versionConfigDAO.updateVersionConfigByOsType(versionConfig);
    }
    public List<String> filterOffLineAccount(List<String> accounts) {
        List<String> temp = new ArrayList<String>();
        if (accounts == null || accounts.size() == 0)
            return null;
        for (int i = 0; i < accounts.size() - 1; i++) {
            Long current = DateUtil.getTimeStampByDate(new Date());
            if (current - getRecentYyLoginTime(accounts.get(i)) < 5 * 60 * 1000) {
                temp.add(accounts.get(i));
            }
        }
        return temp;

    }
    public Long getRecentYyLoginTime(String yyid) {
        initRedis();
        String key = String.format(YY_ACCOUNT_LOGINTIME, yyid);
        String time = redisOperator.get(key, DBUserLevelCached.CACHED_DB_INDEX);
        if (time != null)
            return Long.parseLong(time);
        return 0l;
    }


    @Override
    public String getBusinessAccounts() {
        initRedis();
        String accountstr = redisOperator.get(MAJIA_ACCOUNT_KEY, DBUserLevelCached.CACHED_DB_INDEX);
        if(StringUtils.isBlank(accountstr)||"null".equals(accountstr)){
            return  null;
        }
        String[] accounts = accountstr.split(",");
        List<String> accs = Arrays.asList(accounts);
        List<String> after = filterOffLineAccount(accs);
        if (after == null || after.isEmpty())
            after = accs;
        StringBuffer returnStr = new StringBuffer("");
        for (int i = 0; i < after.size(); i++) {
            returnStr.append(after.get(i));
            if (i != after.size() - 1)
                returnStr.append(",");
        }
        return accountstr;
    }

    @Override
    public void upateBusinessAccount(String accounts) {
        initRedis();
        redisOperator.set(MAJIA_ACCOUNT_KEY, accounts, DBUserLevelCached.CACHED_DB_INDEX);
    }


}
